package com.gsc.controller;

import com.gsc.domain.ResponseResult;
import com.gsc.domain.entity.User;
import com.gsc.enums.AppHttpCodeEnum;
import com.gsc.exception.SystemException;
import com.gsc.service.BlogLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gscstart
 * @create 2023/2/2-16:27
 */
@RestController
public class BlogLoginController {

    @Autowired
    private BlogLoginService blogLoginService;

    /**
     * 前端用户登录接口
     * @RequestBody告诉springmvc在请求体中获取参数
     * @param user
     * @return
     */

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        //前端必须传用户名过来，在这做个判断
        if(!StringUtils.hasText(user.getUserName())){
            //提示 必须要传用户名
//            throw new RuntimeException() 出现一个异常就throw，很麻烦 所以要做异常统一处理
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return blogLoginService.login(user);
    }
    @PostMapping("/logout")
    public ResponseResult logout(){
        return blogLoginService.logout();
    }

}
